/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/12/9 8:28 下午
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.spring.aop.monitor;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  记录业务方法执行信息
 *  如执行耗时等
 * </p>
 *
 * @author Yi
 * @version V1.0.0
 * @date 2019/12/9 8:28 下午
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人}  2019/12/9
 * @modify reason: {方法名}:{原因}
 * ...
 */
@Setter
@Getter
@Slf4j
public class BusinessLogHandler {

    private long begin;
    private long end;
    private String serviceMethod;

    /**
     * 构造函数
     * 记录开始时间和业务方法名称
     * @param serviceMethod
     */
    public BusinessLogHandler(String serviceMethod) {
        this.serviceMethod = serviceMethod;
        setBegin(System.currentTimeMillis());
    }

    /**
     *
     * 记录结束时间
     * 打印业务方法执行耗时日志
     */
    public void businessLog() {
        setEnd(System.currentTimeMillis());
        log.info(getServiceMethod() + "执行耗时" + getElapse() + "毫秒");
    }

    /**
     * 计算耗时
     * @return 方法运行耗时
     */
    private long getElapse() {
        return getEnd() - getBegin();
    }
}
